package siruna.WE.DeviceDependent.rendering;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import siruna.WE.DeviceDependent.AndroidApplication;
import siruna.WE.DeviceDependent.DeviceInfo;
import siruna.WE.DeviceDependent.Global;

import android.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;

public class OverView extends Activity {
	HashMap<Integer, InputStream> icons;
	DeviceInfo di;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main);
		di = new DeviceInfo(this);

		icons = ((AndroidApplication) getApplication()).getIcons();

		ScrollView sv = new ScrollView(this);
		sv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				android.view.ViewGroup.LayoutParams.FILL_PARENT));
		sv.setVerticalScrollBarEnabled(true);

		TableLayout layout = new TableLayout(this);

		layout.setId(1);

		layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				android.view.ViewGroup.LayoutParams.FILL_PARENT));
		layout.setFocusable(true);

		Set<Integer> ids = icons.keySet();

		Iterator<Integer> it = ids.iterator();

		System.out.println("size: " + icons.size());

		if (icons.size() > 1) {
			for (int i = 0; i < icons.size() - 1; i += 2) {
				appendRow(layout, it.next(), it.next());
			}
		}
		if (icons.size() % 2 == 1) {
			appendRow(layout, it.next(), -1);
		}
		System.out.println("hoogte: " + di.getScreenHeight());

		sv.addView(layout);

		setContentView(sv);
	}

	private void appendRow(TableLayout table, int i, int j) {
		TableRow row = new TableRow(this);

		ImageView iv1 = getImageView(i);
		row.addView(iv1, new TableRow.LayoutParams(1));

		if (j != -1) {
			ImageView iv2 = getImageView(j);
			row.addView(iv2, new TableRow.LayoutParams());
		}

		row.setPadding(0, 15, 0, 15);

		table.addView(row, new TableLayout.LayoutParams());
	}

	private ImageView getImageView(int i) {
		Bitmap b = BitmapFactory.decodeStream(icons.get(i));
		ImageView iv = new ImageView(this);

		if (b.getWidth() != Global.ICON || b.getHeight() != Global.ICON) {
			b = b.createScaledBitmap(b, Global.ICON, Global.ICON, true);
		}
		iv.setImageBitmap(b);
		iv.setId(i);
		iv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				setResult(RESULT_OK, (new Intent()).putExtra("widgetId", v
						.getId()));
				finish();

			}
		});

		int padleft = (di.getScreenWidth() - 2 * Global.ICON) / 3;
		iv.setPadding(padleft, 0, 0, 0);

		return iv;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);

		MenuItem item = menu.add("Close");
		// item.setIcon(R.drawable.paint);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);

		if (item.getTitle().equals("Close")) {
			this.setResult(Global.SUBACTIVITY_RESULTCODE_CHAINCLOSE);
			this.finish();
		}
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) {
		case Global.SUBACTIVITY_RESULTCODE_CHAINCLOSE:
			this.setResult(Global.SUBACTIVITY_RESULTCODE_CHAINCLOSE);
			this.finish();
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}